package com.hpch.business.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hpch.business.entity.Product;
import com.hpch.business.mapper.HonorMapper;
import com.hpch.business.service.HonorService;
import com.hpch.business.entity.Honor;
import com.hpch.business.param.HonorParam;
import com.hpch.business.util.RoleUtil;
import com.hpch.common.core.exception.BusinessException;
import com.hpch.common.core.web.PageParam;
import com.hpch.common.core.web.PageResult;
import com.hpch.common.system.entity.Role;
import com.hpch.common.system.entity.User;
import com.hpch.common.system.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 荣誉资质Service实现
 *
 * @author Lly
 * @since 2025-03-06 11:39:01
 */
@Service
public class HonorServiceImpl extends ServiceImpl<HonorMapper, Honor> implements HonorService {


    @Resource
    private UserService userService;


    @Override
    public PageResult<Honor> pageRel(HonorParam param) {
        PageParam<Honor, HonorParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        List<Honor> list = baseMapper.selectPageRel(page, param);
        return new PageResult<>(list, page.getTotal());
    }

    @Override
    public List<Honor> listRel(HonorParam param) {
        List<Honor> list = baseMapper.selectListRel(param);
        // 排序
        PageParam<Honor, HonorParam> page = new PageParam<>();
        page.setDefaultOrder("create_time desc");
        return page.sortRecords(list);
    }

    @Override
    public Honor getByIdRel(Integer id) {
        HonorParam param = new HonorParam();
        param.setId(id);
        return param.getOne(baseMapper.selectListRel(param));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean edit(User user, Honor honor) {
        List<Role> roles = user.getRoles();
        if (RoleUtil.isCompany(roles)) {
            honor.setUserId(user.getUserId());
            honor.setUId(user.getUId());
        } else {
            User oldUser = userService.getById(honor.getUserId());
            if (oldUser == null) {
                throw new BusinessException("用户不存在");
            }
            honor.setUserId(oldUser.getUserId());
            honor.setUId(oldUser.getUId());
        }
        if (honor.getId() == null) {
            return save(honor);
        }
        updateById(honor);

        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateStatus(Integer id) {
        // 根据id获取产品
        Honor oldHonor = getById(id);
        if (oldHonor == null) {
            throw new BusinessException("荣誉资质信息不存在");
        }

        // 修改状态
        Honor update = new Honor();
        update.setId(id);
        update.setStatusFlag(oldHonor.getStatusFlag() == 1 ? 0 : 1);
        updateById(update);

        return true;
    }
}
